#ifndef CAMERA_H
#define CAMERA_H

#include <QMatrix4x4>
#include <QQuaternion>
#include <QVector2D>
#include <QVector3D>
#include <math.h>
#include <QtMath>

// Defines several possible options for camera movement. Used as abstraction to stay away from window-system specific input methods
enum Camera_Movement {
    FORWARD,
    BACKWARD,
    LEFT,
    RIGHT
};

// Default camera values
const float YAW        = -90.0f;
const float PITCH      =  0.0f;
const float SPEED      =  2.5f;
const float SENSITIVTY =  0.2f;
const float ZOOM       =  45.0f;

// An abstract camera class that processes input and calculates the corresponding Eular Angles, Vectors and Matrices for use in OpenGL
class Camera
{
public:
    // Camera Attributes
    QVector3D Position;
    QVector3D Front;
    QVector3D Up;
    QVector3D Right;
    QVector3D WorldUp;

    // Eular Angles
    float Yaw;   // 偏航角
    float Pitch; // 俯仰角
    // Camera options
    float MovementSpeed;
    float MouseSensitivity;
    float Zoom;

    // Constructor with vectors
    Camera(QVector3D position = QVector3D(0.0f, 0.0f, 0.0f), QVector3D up = QVector3D(0.0f, 1.0f, 0.0f), float yaw = YAW, float pitch = PITCH) : Front(QVector3D(0.0f, 0.0f, -1.0f)), MovementSpeed(SPEED), MouseSensitivity(SENSITIVTY), Zoom(ZOOM)
    {
        Position = position;
        WorldUp = up;
        Yaw = yaw;
        Pitch = pitch;
        updateCameraVectors();
    }
    // Constructor with scalar values
    Camera(float posX, float posY, float posZ, float upX, float upY, float upZ, float yaw, float pitch) : Front(QVector3D(0.0f, 0.0f, -1.0f)), MovementSpeed(SPEED), MouseSensitivity(SENSITIVTY), Zoom(ZOOM)
    {
        Position = QVector3D(posX, posY, posZ);
        WorldUp = QVector3D(upX, upY, upZ);
        Yaw = yaw;
        Pitch = pitch;
        updateCameraVectors();
    }

    // Returns the view matrix calculated using Eular Angles and the LookAt Matrix
    QMatrix4x4 GetViewMatrix()
    {
        QMatrix4x4 la;
        la.setToIdentity();
        la.lookAt(Position, Position + Front, Up);
        return la;
    }

    // Processes input received from any keyboard-like input system. Accepts input parameter in the form of camera defined ENUM (to abstract it from windowing systems)
    void ProcessKeyboard(Camera_Movement direction, float deltaTime)
    {
        float velocity = MovementSpeed * deltaTime;
        if (direction == FORWARD)
            Position += Front * velocity;
        if (direction == BACKWARD)
            Position -= Front * velocity;
        if (direction == LEFT)
            Position -= Right * velocity;
        if (direction == RIGHT)
            Position += Right * velocity;
    }

    void initFpsCamera() {
        float posLen = Position.length();
        Position = QVector3D(0.0f, 0.0f, posLen);
        Yaw = YAW;
        Pitch = PITCH;
        updateCameraVectors();
    }

    void setPosition(const QVector3D& pos) {
        if (pos.x() == pos.length()) {
            Yaw = 2 * YAW;
            Pitch = 0.0f;
        } else if (pos.x() == -pos.length()) {
            Yaw = 0.0f;
            Pitch = 0.0f;
        } else if (pos.y() == pos.length()) {
            Yaw = YAW;
            Pitch = -89.0f;
        } else if (pos.y() == -pos.length()) {
            Yaw = YAW;
            Pitch = 89.0f;
        } else if (pos.z() == pos.length()) {
            Yaw = YAW;
            Pitch = 0.0f;
        } else if (pos.z() == -pos.length()) {
            Yaw = -YAW;
            Pitch = 0.0f;
        }
        Position = pos;
        updateCameraVectors();
    }

    // Processes input received from a mouse input system. Expects the offset value in both the x and y direction.
    void ProcessMouseMovement(float xoffset, float yoffset, GLboolean constrainPitch = true)
    {
        xoffset *= MouseSensitivity;
        yoffset *= MouseSensitivity;

        Yaw   += xoffset;
        Pitch += yoffset;

        // Make sure that when pitch is out of bounds, screen doesn't get flipped
        if (constrainPitch)
        {
            if (Pitch > 89.0f)
                Pitch = 89.0f;
            if (Pitch < -89.0f)
                Pitch = -89.0f;
        }

        // Update Front, Right and Up Vectors using the updated Eular angles
        updateCameraVectors();
    }

    void ProcessRotation(float &xoffset, float &yoffset) {
        if (-yoffset >= 89.0f)
            yoffset = -89.0f;
        if (-yoffset <= -89.0f)
            yoffset = 89.0f;
        float posLen = Position.length();
        Position.setX(posLen * cos(qDegreesToRadians(-yoffset)) * sin(qDegreesToRadians(-xoffset)));
        Position.setY(posLen * sin(qDegreesToRadians(-yoffset)));
        Position.setZ(posLen * cos(qDegreesToRadians(-yoffset)) * cos(qDegreesToRadians(-xoffset)));

        adjustCameraVectors();
    }

    // Processes input received from a mouse scroll-wheel event. Only requires input on the vertical wheel-axis
    void ProcessMouseScroll(float yoffset)
    {
        if (Zoom >= 1.0f && Zoom <= 45.0f)
            Zoom -= yoffset;
        if (Zoom <= 1.0f)
            Zoom = 1.0f;
        if (Zoom >= 45.0f)
            Zoom = 45.0f;
    }

private:
    // Calculates the front vector from the Camera's (updated) Eular Angles
    void updateCameraVectors()
    {
        // Calculate the new Front vector
        QVector3D front;
        front.setX(cos(qDegreesToRadians(Yaw)) * cos(qDegreesToRadians(Pitch)));
        front.setY(sin(qDegreesToRadians(Pitch)));
        front.setZ(sin(qDegreesToRadians(Yaw)) * cos(qDegreesToRadians(Pitch)));
        Front = front.normalized();
        // Also re-calculate the Right and Up vector
        Right = QVector3D::crossProduct(Front, WorldUp).normalized();  // Normalize the vectors, because their length gets closer to 0 the more you look up or down which results in slower movement.
        Up    = QVector3D::crossProduct(Right, Front).normalized();
    }

    void adjustCameraVectors() {
        QVector3D front = QVector3D(0.0f, 0.0f, 0.0f) - Position;
        Front = front;
        Right = QVector3D::crossProduct(Front, WorldUp).normalized();
        Up    = QVector3D::crossProduct(Right, Front).normalized();
    }
};
#endif
